/**
 *
 */
package com.angel.data.generator.checkers.impl;

import com.angel.data.generator.checkers.DataGeneratorChecker;
import com.angel.data.generator.exceptions.DataGeneratorCheckerException;
import com.angel.data.generator.exceptions.DataGeneratorException;
import com.angel.data.generator.interfaces.DataGenerator;
import com.angel.data.generator.repository.DataGeneratorRepository;

/**
 * @author William
 *
 */
public class InverseDependenceDataGeneratorChecker implements DataGeneratorChecker{

	public void checkDataGenerator(DataGenerator dataGenerator, DataGenerator[] dependencies) throws DataGeneratorException {
		Class<? extends DataGenerator>[] dependecesGenerators = dataGenerator.getDependecesGenerators();
		if(dependecesGenerators != null){
			for(Class<? extends DataGenerator> dependecesGeneratorClass: dependecesGenerators){
				if(this.dependsOnDependencies(dependecesGeneratorClass, dataGenerator, dependencies)){
					throw new DataGeneratorCheckerException("Data generator class [" + dataGenerator.getClass() + "] has" +
							" an inverse dependence with data generator class [" + dependecesGeneratorClass + "]");
				}
			}
		}
	}

	private boolean dependsOnDependencies(Class<? extends DataGenerator> dataGeneratorClassDependenceOn, DataGenerator dataGeneratorDepender, DataGenerator[] dependencies){
		//TODO Verify singleton instance.
		DataGenerator[] currentDependences = DataGeneratorRepository.getInstance().getDependenciesFor(dataGeneratorClassDependenceOn);
		if(currentDependences != null && currentDependences.length > 0){
			for(DataGenerator currentDependence: currentDependences){
				if(currentDependence.getClass().equals(dataGeneratorDepender.getClass())){
					return true;
				}
			}
		}
		return false;
	}

}
